const axios = require('axios');

(async () => {
  console.log('🎖️ 测试勋章生成功能...');
  
  try {
    // 1. 登录用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'alice@student.com',
      password: 'password123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 用户登录成功: ID=${userId}`);
    
    // 2. 创建一个正在进行的活动
    console.log('\n📅 创建正在进行的活动...');
    
    const now = new Date();
    const startTime = new Date(now.getTime() - 5 * 60 * 1000); // 5分钟前开始
    const endTime = new Date(now.getTime() + 55 * 60 * 1000);  // 55分钟后结束
    
    const activity = {
      title: '勋章测试活动',
      description: '专门用于测试勋章生成功能的活动',
      location: '测试地点',
      start_time: startTime.toISOString(),
      end_time: endTime.toISOString(),
      max_participants: 50,
      category_id: 1,
      requirements: '测试勋章生成',
      latitude: 39.90420000,
      longitude: 116.40740000
    };
    
    const activityResponse = await axios.post('http://localhost:3000/api/activities', activity, {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    const activityId = activityResponse.data.activity.id;
    console.log('✅ 活动创建成功:', activityId);
    
    // 3. 参与活动
    console.log('\n🎯 参与活动...');
    const joinResponse = await axios.post(`http://localhost:3000/api/activities/${activityId}/join`, {}, {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    console.log('✅ 参与成功:', joinResponse.data.message);
    
    // 4. 签到获得勋章
    console.log('\n📝 签到获得勋章...');
    const checkinResponse = await axios.post(`http://localhost:3000/api/activities/${activityId}/checkin`, {
      latitude: 39.90420000,
      longitude: 116.40740000
    }, {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    console.log('✅ 签到成功:', checkinResponse.data.message);
    console.log('🎖️ 勋章信息:', checkinResponse.data.badgeMessage);
    
    // 5. 等待勋章生成
    console.log('\n⏳ 等待勋章生成...');
    await new Promise(resolve => setTimeout(resolve, 5000));
    
    // 6. 检查勋章
    console.log('\n🏆 检查勋章生成结果...');
    
    const allBadgesResponse = await axios.get('http://localhost:3000/api/badges');
    console.log('📈 系统总勋章数量:', allBadgesResponse.data.badges?.length || 0);
    
    const userBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    console.log('🎖️ 用户拥有勋章数量:', userBadgesResponse.data.badges?.length || 0);
    
    if (userBadgesResponse.data.badges?.length > 0) {
      console.log('\n🎉 勋章生成成功！');
      userBadgesResponse.data.badges.forEach((badge, index) => {
        console.log(`   勋章 ${index + 1}:`);
        console.log(`     - 名称: ${badge.badge_name}`);
        console.log(`     - 描述: ${badge.badge_description}`);
        console.log(`     - 稀有度: ${badge.rarity}`);
        console.log(`     - 状态: ${badge.status}`);
        console.log(`     - 铸造时间: ${badge.minted_at}`);
      });
    } else {
      console.log('❌ 勋章生成失败或尚未完成');
    }
    
  } catch (error) {
    console.error('❌ 测试过程出错:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 